import {
    _decorator,
    Collider2D,
    Component,
    Contact2DType,
    director,
    ERaycast2DType,
    geometry,
    input,
    Input,
    IPhysics2DContact,
    KeyCode,
    Node,
    PhysicsSystem2D,  // 改用 PhysicsSystem2D
    RigidBody2D,
    UITransform,
    Vec2,
    Vec3,
  } from "cc";
  const { ccclass, property } = _decorator;
  
  @ccclass("birdo")
  export class birdo extends Component {
    private rigidBody: RigidBody2D = null!;
  
    private readonly JUMP_FORCE = 0.5; // 跳跃力度
    private readonly HORIZONTAL_SPEED = 0; // 水平移动速度
     // 移动方向 1表示向上，-1表示向下
  private moveDirection = 1;
  private readonly MOVE_SPEED = 100;
    dir:Vec2 =new Vec2(0,1)
    start() {
      PhysicsSystem2D.instance.enable = true;
      // 获取刚体组件
      this.rigidBody = this.getComponent(RigidBody2D);
      // 设置初始水平速度
      this.rigidBody.linearVelocity = new Vec2(this.HORIZONTAL_SPEED, 0);
      // input.on(Input.EventType.KEY_DOWN, (event) => {
      //   if (event.keyCode === KeyCode.SPACE) {
      //     //向上移动当前距离
      //     this.jump();
      //   }
      // });
  
      // const collider = this.getComponent(Collider2D);
      // collider.on(Contact2DType.BEGIN_CONTACT, this.onBeinContact, this);
      // collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
    
    }
    private jump() {
      // 重置垂直速度为0，确保每次跳跃的力度一致
      this.rigidBody.linearVelocity = new Vec2(this.HORIZONTAL_SPEED, 0);
      // 施加向上的力
      this.rigidBody.applyLinearImpulse(
        new Vec2(0, this.JUMP_FORCE),
        this.rigidBody.getWorldCenter(new Vec2()),
        true
      );
    }
  
    private gameOver() {
      // 停止小鸟的移动
      this.rigidBody.linearVelocity = Vec2.ZERO;
      // 禁用输入监听
      input.off(Input.EventType.KEY_DOWN);
      // 这里可以触发游戏结束事件
      console.log("Game Over!");
    }
    onBeinContact(
      selfCollider: Collider2D,
      otherCollider: Collider2D,
      contact: IPhysics2DContact
    ) {
      if (otherCollider.tag === 1) {
        this.gameOver();
        console.log("碰撞到地面,点位", contact.getWorldManifold().points[0]);
      }
    }
    onEndContact(selfCollider, otherCollider) {
      // console.log("碰撞结束");
    }
    update(deltaTime: number) {
      // 获取当前位置
    const currentPos = this.node.getPosition();
    const worldPos = this.node.worldPosition;
      // 2D射线检测
      const p1 = new Vec2(
        worldPos.x,
        worldPos.y
      );
         // 向当前移动方向发射射线
    const rayLength = 50; // 射线长度
      const p2 = new Vec2(
        worldPos.x,
        worldPos.y +(rayLength*this.moveDirection)
      );
  
      const results = PhysicsSystem2D.instance.raycast(
        p1,
        p2,
        ERaycast2DType.Any
      );
        // 如果检测到碰撞，改变方向
        if (results.length > 0) {
          this.moveDirection *= -1; // 改变方向
          console.log('检测到墙体，改变方向',results);
        }
     // 更新位置
     const newPosition = new Vec3(
        currentPos.x,
        currentPos.y + (this.moveDirection * this.MOVE_SPEED * deltaTime),
        currentPos.z
      );
      
      // 设置新位置
      this.node.setPosition(newPosition);
    }


    
  }
  